//
//  StoreGoodsCell.m
//  KYRR
//
//  Created by kuyuZJ on 2017/4/22.
//
//

#import "StoreGoodsCell.h"
#import "VerticallyAlignedLabel.h"

@interface StoreGoodsCell ()
@property(nonatomic,strong) UIImageView* thumbnail;
@property(nonatomic,strong) UIImageView* imgRecommand;
@property(nonatomic,strong) UIImageView* imgMiaoSha;
@property(nonatomic,strong) UIView* rightView;
@property(nonatomic,strong) UILabel* labelGoodsName;
@property(nonatomic,strong) VerticallyAlignedLabel* labelSummary;
@property(nonatomic,strong) UILabel* labelSale;
@property(nonatomic,strong) UILabel* labelPrice;
@property(nonatomic,strong) UIButton* btnAdd;

@end

@implementation StoreGoodsCell{
    CGSize thumnailSize,addSize,recommandSize,miaoShaSize;
    CGFloat titleHeight,saleHeight,pricerHeight;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        thumnailSize = CGSizeMake(70.f, 70.f);
        addSize = CGSizeMake(35.f, 35.f);
        recommandSize = CGSizeMake(20.f, 20.f);
        miaoShaSize = CGSizeMake(30.f, 28.f);
        titleHeight = 20.f;saleHeight = 15.f;pricerHeight = 20.f;
        [self.contentView addSubview:self.thumbnail];
        [self.contentView addSubview:self.imgRecommand];
        [self.contentView addSubview:self.imgMiaoSha];
        [self.contentView addSubview:self.rightView];
        [self.rightView addSubview:self.labelGoodsName];
        [self.rightView addSubview:self.labelSummary];
        [self.rightView addSubview:self.labelSale];
        [self.rightView addSubview:self.labelPrice];
        [self.contentView addSubview:self.btnAdd];
        
        NSArray<NSString*>* formats = @[@"H:|-leftEdge-[thumbnail(==thumnailWidth)]-leftEdge-[rightView]-leftEdge-|",
                                        @"V:|-topEdge-[thumbnail(==thumbnailHeight)]", @"V:|-topEdge-[rightView]-topEdge-|",
                                        @"H:[imgRecommand(==recommandWidth)]-5-[rightView]", @"V:|-5-[imgRecommand(==recommandHeight)]",
                                        
                                        @"H:|-5-[imgMiaoSha(==miaoShaWidth)]", @"V:|-5-[imgMiaoSha(==miaoShaHeight)]",
                                        
                                        @"H:|-defEdge-[labelGoodsName]-defEdge-|",
                                        //@"H:|-defEdge-[labelSummary]-defEdge-|",[labelSummary]-itemSpace-
                                        @"H:|-defEdge-[labelSale]-defEdge-|",@"H:|-defEdge-[labelPrice]-defEdge-|",
                                        @"V:|-defEdge-[labelGoodsName]-itemSpace-[labelSale]-itemSpace-[labelPrice]-defEdge-|",
                                        @"H:[btnAdd(==addWidth)]-5-|", @"V:[btnAdd(==addHeight)]-5-|"];
        NSDictionary<NSString*,id>* metrics = @{ @"defEdge":@(0), @"leftEdge":@(10), @"topEdge":@(10), @"itemSpace":@(5.f),
                                                 @"thumnailWidth":@(thumnailSize.width), @"thumbnailHeight":@(thumnailSize.height),
                                                 @"addWidth":@(addSize.width), @"addHeight":@(addSize.height),
                                                  @"recommandWidth":@(recommandSize.width), @"recommandHeight":@(recommandSize.height),
                                                  @"miaoShaWidth":@(miaoShaSize.width), @"miaoShaHeight":@(miaoShaSize.height)
                                                 };
        NSDictionary<NSString*,id>* views = @{ @"thumbnail":self.thumbnail, @"rightView":self.rightView, @"imgRecommand":self.imgRecommand, @"imgMiaoSha":self.imgMiaoSha,
                                               @"labelGoodsName":self.labelGoodsName, @"labelSummary":self.labelSummary, @"labelSale":self.labelSale, @"labelPrice":self.labelPrice,
                                               @"btnAdd":self.btnAdd
                                               };
        [formats enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSLog( @"%@ %@",[self class],obj);
            NSArray<NSLayoutConstraint*>* constraints = [NSLayoutConstraint constraintsWithVisualFormat:obj options:0 metrics:metrics views:views];
            [self.contentView addConstraints:constraints];
        }];

    }
    return  self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setEntity:(MGoods *)entity{
    if(entity){
        _entity = entity;
        [self.thumbnail sd_setImageWithURL:[NSURL URLWithString:self.entity.defaultImg] placeholderImage:[UIImage imageNamed:kDefaultImage]];
        self.labelGoodsName.text = self.entity.goodsName;
//        self.labelSummary.text = self.entity.summary;
        self.labelSale.text = [NSString stringWithFormat: @"销量 %@",self.entity.sales];
        self.labelPrice.text =[NSString stringWithFormat: @"￥%@", self.entity.price];
        CGFloat price = [entity.price floatValue];
        CGFloat maketPrice = [entity.maketPrice floatValue];
        self.imgRecommand.hidden = price>=maketPrice;
        if([entity.storeStatus integerValue]==1){
            if([entity.stock integerValue]>0){
                self.btnAdd.userInteractionEnabled = YES;
                [self.btnAdd setImage:[UIImage imageNamed:@"icon-add"] forState:UIControlStateNormal];
            }else {
                self.btnAdd.userInteractionEnabled = NO;
                [self.btnAdd setImage:[UIImage imageNamed:@"icon-nothing"] forState:UIControlStateNormal];
            }
        }else{
            self.btnAdd.userInteractionEnabled = NO;
            [self.btnAdd setImage:[UIImage imageNamed:@"icon-off"] forState:UIControlStateNormal];
        }
        if(entity.isMiaoSha){
            self.imgMiaoSha.hidden = NO;
        }else{
            self.imgMiaoSha.hidden = YES;
        }
    }
}

#pragma mark =====================================================  SEL
-(IBAction)btnAddTouch:(id)sender{
    if(self.delegate && [self.delegate respondsToSelector:@selector(storeGoodsAddCar:)]){
        [self.delegate storeGoodsAddCar:self.entity];
    }
}

#pragma mark =====================================================  property package
-(UIImageView *)thumbnail{
    if(!_thumbnail){
        UIImageView* empty = [[UIImageView alloc]init];
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _thumbnail = empty;
    }
    return _thumbnail;
}
-(UIImageView *)imgRecommand{
    if(!_imgRecommand){
        _imgRecommand = [[UIImageView alloc]init];
        [_imgRecommand  setImage:[UIImage imageNamed:@"icon-promotion"]];
        _imgRecommand.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _imgRecommand;
}

-(UIImageView *)imgMiaoSha{
    if(!_imgMiaoSha){
        UIImageView* empty = [[UIImageView alloc]init];
        [empty setImage:[UIImage imageNamed: @"icon-buy-icon"]];
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _imgMiaoSha = empty;
    }
    return _imgMiaoSha;
}

-(UIView *)rightView{
    if(!_rightView){
        UIView* empty = [[UIView alloc]init];
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _rightView = empty;
    }
    return _rightView;
}

-(UILabel *)labelGoodsName{
    if(!_labelGoodsName){
        UILabel* empty = [[UILabel alloc]init];
        empty.font = [UIFont systemFontOfSize:13.f];
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _labelGoodsName = empty;
    }
    return _labelGoodsName;
}

-(VerticallyAlignedLabel *)labelSummary{
    if(!_labelSummary){
        VerticallyAlignedLabel* empty = [[VerticallyAlignedLabel alloc]init];
        empty.textColor = theme_Fourm_color;
        empty.font = [UIFont systemFontOfSize:12.f];
        empty.numberOfLines = 2;
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _labelSummary = empty;
    }
    return _labelSummary;
}

-(UILabel *)labelSale{
    if(!_labelSale){
        UILabel* empty = [[UILabel alloc]init];
        empty.textColor = theme_Fourm_color;
        empty.font = [UIFont systemFontOfSize:12.f];
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _labelSale = empty;
    }
    return _labelSale;
}

-(UILabel *)labelPrice{
    if(!_labelPrice){
        UILabel* empty = [[UILabel alloc]init];
        empty.textColor = [UIColor redColor];
        empty.font = [UIFont boldSystemFontOfSize:20.f];
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _labelPrice = empty;
    }
    return _labelPrice;
}

-(UIButton *)btnAdd{
    if(!_btnAdd){
        UIButton* empty = [UIButton buttonWithType:UIButtonTypeCustom];
        [empty setImage:[UIImage imageNamed: @"icon-addcar"] forState:UIControlStateNormal];
        [empty setImageEdgeInsets:UIEdgeInsetsMake(5.f, 5.f, 5.f, 5.f)];
        [empty addTarget:self action:@selector(btnAddTouch:) forControlEvents:UIControlEventTouchUpInside];
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _btnAdd = empty;
    }
    return _btnAdd;
}


@end
